package com.azt.front.interceptor;

import com.azt.api.enums.CompanyAuditStateEnum;
import com.azt.api.pojo.Company;
import com.azt.front.permission.ClearRequire;
import com.azt.front.permission.RequireCompany;
import com.azt.front.utils.ContextUtil;
import com.azt.utils.CommonUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * 权限拦截器
 * Created by shangwq on 2016/8/7.
 */
public class RequireCompanyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;

            RequireCompany requireCompany = handlerMethod.getMethodAnnotation(RequireCompany.class);
            ClearRequire clearRequire = handlerMethod.getMethodAnnotation(ClearRequire.class);
            if (clearRequire == null && requireCompany == null) {
                requireCompany = handlerMethod.getBean().getClass().getAnnotation(RequireCompany.class);
            }

            if (requireCompany != null) {
                if(!ContextUtil.isLogined()) {
                    CommonUtil.redirectToLogin(request, response);
                    return false;
                }
                Company company = ContextUtil.getCompany();
                boolean pass = false;
                if(company != null && company.getAuditState() != null) {
                    int auditState = company.getAuditState();
                    int[] state = requireCompany.state();
                    int[] noState = requireCompany.noState();
                    if(ArrayUtils.isEmpty(state) && ArrayUtils.isEmpty(noState)) {
                        if(auditState == CompanyAuditStateEnum.PASS.getValue()) {
                            pass = true;
                        }
                    } else if(ArrayUtils.isNotEmpty(state)) {
                        if(ArrayUtils.contains(state, auditState)) {
                           pass = true;
                        }
                    } else if(ArrayUtils.isNotEmpty(noState)) {
                        if(!ArrayUtils.contains(noState, auditState)) {
                            pass = true;
                        }
                    }
                }

                if(!pass) {
                    if (CommonUtil.isAjax(request)) {
                        //ajax请求的话则403状态返回错误码
                        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                        CommonUtil.renderJson(response, "公司状态异常");
                    } else {
                        String url = "/user/companySearch";
                        if(company != null) {
                            url = "/noperm";
                        }
                        String preUrl = CommonUtil.getPreUrl(request);
                        if (StringUtils.isNotBlank(preUrl)) {
                            try {
                                url += "?redirect=" + URLEncoder.encode(preUrl, "utf-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            response.sendRedirect(url);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }
}
